//! system接口，用户、单位、角色、资源权限系统操作相关的路由
use axum::{
    middleware,
    routing::{get, post, put},
    Router,
};

use crate::middleware::operator_middleware;
pub mod dept;
pub mod dict;
pub mod log;
pub mod post;
pub mod resource;
pub mod role;
pub mod user;

/// 用户、单位、角色、资源权限系统操作相关的路由
/// - `{prefix}/user/` `GET|POST` [User](ane_system::entity::sys_user)的查询和创建
/// - `{prefix}/user/:id` `GET|PUT|DELETE` [User](ane_system::entity::sys_user)的更新和删除
/// - `{prefix}/assign_role` `POST` 分配角色
/// - `{prefix}/user/update_password` `POST` 直接修改用户的密码
/// - `{prefix}/dept/` `GET|POST` [Dept](ane_system::entity::sys_dept)的查询和创建
/// - `{prefix}/dept/:id` `GET|PUT|DELETE` [Dept](ane_system::entity::sys_dept)的获取、更新和删除
/// - `{prefix}/role/` `GET|POST` [Role](ane_system::entity::sys_role)的查询和创建
/// - `{prefix}/role/:id` `PUT|DELETE` [Role](ane_system::entity::sys_role)的更新和删除
/// - `{prefix}/role/resource/:id` `GET|PUT` [Role](ane_system::entity::sys_role)资源的获取和更新
/// - `{prefix}/` `GET|POST` [Post](ane_system::entity::sys_post)的查询和创建
/// - `{prefix}/:id` `GET|PUT|DELETE` [Post](ane_system::entity::sys_post)的更新和删除
/// - `{prefix}/resource/` `GET|POST` [Resource](ane_system::entity::sys_resource)的查询和创建
/// - `{prefix}/resource/:id` `PUT|DELETE` [Resource](ane_system::entity::sys_resource)的更新和删除
/// - `{prefix}/dict/` `GET|POST` [Dict](ane_system::entity::sys_dict)的查询和创建
/// - `{prefix}/dict/:id` `PUT|DELETE` [Dict](ane_system::entity::sys_dict)的更新和删除
/// - `{prefix}/dict/:dict_id/` `GET|POST` [DictData](ane_system::entity::sys_dict_data)的更新和删除
/// - `{prefix}/dict/:dict_id/:id` `PUT|DELETE` [DictData](ane_system::entity::sys_dict_data)的更新和删除
/// - `{prefix}/log/login` `GET|DELETE` [LoginLog](ane_system::entity::sys_login_log)的查询和删除
/// - `{prefix}/log/operate` `GET|DELETE` [OperateLog](ane_system::entity::sys_operate_log)的查询和删除
pub fn get_route<S>(prefix: &str) -> Router<S>
where
    S: Sync + Send + Clone + 'static,
{
    let f = move |p: &str| -> String { format!("{}{p}", prefix) };
    Router::new()
        // 用户管理
        .merge(user::get_route(&f("/user")))
        // 部门管理
        .merge(dept::get_route(&f("/dept")))
        // 资源管理
        .merge(resource::get_route(&f("/resource")))
        // 角色管理
        .merge(role::get_route(&f("/role")))
        // 岗位管理
        .merge(post::get_route(&f("/post")))
        // 字典管理
        .merge(dict::get_route(&f("/dict")))
        // 日志记录
        .merge(log::get_route(&f("/log")))
        .route_layer(middleware::from_fn(operator_middleware))
}

/// system接口list整合起来的参数
#[derive(Debug, serde::Deserialize)]
pub struct SysFilter {
    id: Option<i32>,
    order: Option<String>,
    dept_id: Option<i32>,
    username: Option<String>,
    nickname: Option<String>,

    key: Option<String>,
    name: Option<String>,

    path: Option<String>,

    dict_name: Option<String>,
    dict_type: Option<String>,

    result: Option<i8>,

    status: Option<bool>,
    #[serde(flatten)]
    page: common::page::PageParams,
}
